window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js'], function(Z, I, M, U) {
		var curPage = 1;
		var pageSize = 10;
		var totalPage = 0;
		var keyword = "";
		var typ = 0;
		var state = 0; //-2-全部，1-未开始，2-进行中，5-审核中，4-已完成//默认不显示已完成
		var provinceId = 1; //1(1-默认排序,2-时间排序)
		var fist=true;
		var cityId=0;
		U.fillHead();
		U.fillFooter();
		var userid = U.getParam("userId");
		if(userid == null || userid ==""){
			userid = 0;
		}
		initPage();
		function initPage() { //初始化页面
			keyword = U.getParam("keyword");
			$(".name").val(keyword);
			U.loadOpt(false);
			getlist();
		}
		function initEvent() {
			$(".btn").unbind("click").click(function() {
				keyword = $(".name").val();
				getlist();
			});
			$(".classify>span").unbind("click").click(function() {
				if($(this).index()==0){
					return false;
				}
				cityId=$(this).attr("id");
				$(".classify>span").css("background-color", "white");
				$(".classify>span").css("color", "black");
				$(this).css("background-color", "#E13C44");
				$(this).css("color", "white");
				getlist();
			});
			$(".orderlist").unbind("click").click(function() {
				provinceId = $(this).attr("order");
				getlist();
			});
			$(".state>span").unbind("click").click(function() {
				var index = $(this).attr("indexType");
				console.log(index);
				if(index == -2) {
					return false;
				}
				$(".state>span").css("background-color", "white");
				$(".state>span").css("color", "black");
				$(this).css("background-color", "#E13C44");
				$(this).css("color", "white");
				state = index ;
				getlist();
			});
			$(".project_list>div").width((($(".project_list").width()) - 24 * 4 - 2 * 3) / 3);
			$(".project_list>div").unbind("click").click(function() {
				window.open("project_details.html?id=" + $(this).attr("id"));
			});
		}

		function paging_handle(page, method) {
		
			//分页导航数据处理
			totalPage = page.totalPage;
			$("#paging .page").empty();
			var start_count = 0;
			try {
				if(curPage >= 10) {
					start_count = curPage - 5;
				}
			} catch(e) {}
			var lastsize = totalPage;
			lastsize = start_count + 10;
			
			if(lastsize>=totalPage){
				lastsize=totalPage;
			}
			for(var i = start_count; i < lastsize; i++) {
				if(i + 1 == curPage) {
					$("#paging .page").append("<div style='color:white;border-color:white;background-color:#DC3F44'>" + (i + 1) + "</div>");
				} else {
					$("#paging .page").append("<div>" + (i + 1) + "</div>");
				}
			}
			$(".Record>span:nth-child(1)").text("共" + page.totalItem + "条记录");
			$(".Record>span:nth-child(2)").text(curPage + "/" + page.totalPage);
			$("#paging .goto_page>input").val(curPage);
			//分页导航数据处理完毕
			//处理单击事件
			$(".next_page").unbind("click").click(function() {
				if(!page.nextPage) {
					alert("已经是最后一页了!");
					return false;
				}
				curPage++;
				method();
			});
			$(".up_page").unbind("click").click(function() {
				if(curPage == 1) {
					alert("已经是第一页了!");
					return false;
				}
				curPage--;
				method();
			});
			$("#paging .page>div").unbind("click").click(function() {
				curPage = $(this).text();
				method();
			});
			$("#paging .goto").unbind("click").click(function() {
				try {
					var index = $("#paging .goto_page>input").val();
					if(index <= totalPage && index > 0 && !/^[+-]?[1-9]?[0-9]*\.[0-9]*$/.test(index)) {
						curPage = $("#paging .goto_page>input").val();
						method();
					} else {
						alert("请输入正确的页码！");
					}
				} catch(e) {
					alert("请输入正确的页码！");
				}
			});
			//单机事件绑定完毕
		
		
		}
		//分页方法
		function getlist() {
			var params = {
				curPage: curPage,
				pageSize: 10,
				name: keyword,
				typ: typ,
				cityId:cityId,
				userId:userid,
				state: state, //-2-全部，1-未开始，2-进行中，5-审核中，4-已完成
				provinceId: provinceId //1(1-默认排序,2-时间排序)
			};
			U.loadOpt(false);
			U.ajax("/project/list", params, function(json) {
				if(json.success) {
					if(fist){
						fist=false;
						$(json.models.categoryList).each(function(){
							$(".classify").append("<span id='"+this.id+"'>"+this.name+"</span>");
						});
						
					}
					$(".project_list").empty();
					$(".project_list").hide();
					$(json.models.projectList).each(function() {
						var p_state = "项目未开展";
						if(this.state == 2) {
							p_state = "项目进行中";
						}
						if(this.state == 4) {
							p_state = "项目已完成";
						}
						var p_img = U.loadfail;
						if(U.getImg(this.images).length > 0) {
							p_img = U.getImg(this.images)[0];
						}
						var p = "<div id='" + this.id + "' ><div class='p_name'>" + this.name + "</div><div class='subtitle'>" + this.title + "</div><div class='p_img'><img src='" + p_img + "'></div><div class='help_info'>【扶贫信息】</div><div class='help_name'>" + this.povertyName + "</div><div class='money'></div><div class='p_state'>" + p_state + "</div></div>";
						$(".project_list").append(p);
					});
					paging_handle(json.models.page, getlist); //分页调用方法
					U.loadOpt(false);
					$(".project_list").show(50, function() {
						initEvent();
					});
				} else {
					M.toast(false, json.errorMsg);
				}
			});
		}
	});
};